{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 决策树"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "150c3d01a2cad240"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-22T12:58:53.886247Z",
     "start_time": "2025-05-22T12:58:53.080991Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle\n",
    "import os\n",
    "import time\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score, precision_recall_fscore_support, confusion_matrix, classification_report\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 加载数据及数据预处理"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cbc7ff96db2f0f6e"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 数据集路径\n",
    "data_dir = r'D:\\Machine_learning\\jiqixuexi\\cifar-10-python\\cifar-10-batches-py'\n",
    "\n",
    "# 加载CIFAR-10数据集\n",
    "def load_cifar10_data(data_dir):\n",
    "    # 加载训练数据\n",
    "    train_data = []\n",
    "    train_labels = []\n",
    "    for i in range(1, 6):\n",
    "        batch_path = os.path.join(data_dir, f'data_batch_{i}')\n",
    "        with open(batch_path, 'rb') as file:\n",
    "            batch = pickle.load(file, encoding='latin1')\n",
    "            train_data.append(batch['data'])\n",
    "            train_labels.extend(batch['labels'])\n",
    "    train_data = np.array(train_data).reshape(50000, 3, 32, 32).transpose(0, 2, 3, 1)\n",
    "    train_labels = np.array(train_labels)\n",
    "\n",
    "    # 加载测试数据\n",
    "    test_batch_path = os.path.join(data_dir, 'test_batch')\n",
    "    with open(test_batch_path, 'rb') as file:\n",
    "        test_batch = pickle.load(file, encoding='latin1')\n",
    "    test_data = test_batch['data'].reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1)\n",
    "    test_labels = np.array(test_batch['labels'])\n",
    "\n",
    "    return (train_data, train_labels), (test_data, test_labels)\n",
    "\n",
    "# 调用函数加载数据\n",
    "(train_data, train_labels), (test_data, test_labels) = load_cifar10_data(data_dir)\n",
    "\n",
    "# 数据预处理\n",
    "train_data = train_data.astype('float32') / 255.0\n",
    "test_data = test_data.astype('float32') / 255.0\n",
    "\n",
    "# 将图像数据展平为一维向量\n",
    "train_data = train_data.reshape(-1, 32*32*3)\n",
    "test_data = test_data.reshape(-1, 32*32*3)\n",
    "\n",
    "# 使用PCA降维\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "# 保留95%的方差\n",
    "pca = PCA(n_components=0.95)\n",
    "train_data = pca.fit_transform(train_data)\n",
    "test_data = pca.transform(test_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-05-22T13:13:12.366160Z",
     "start_time": "2025-05-22T12:59:01.453582Z"
    }
   },
   "id": "8bf8cd83a058dbe",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 创建模型、模型训练与评估预测"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "229e5b58bd749c72"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练时间: 15.45秒\n",
      "Accuracy: 0.3105\n",
      "Precision: 0.31178222068362865\n",
      "Recall: 0.3105\n",
      "F1-score: 0.30709089019390856\n",
      "Confusion Matrix:\n",
      " [[456  63  71  50  48  18  30  32 156  76]\n",
      " [ 64 323  32  57  92  32  80  28  97 195]\n",
      " [108  32 249  69 202  67 136  59  35  43]\n",
      " [ 62  63  66 159 186 131 142  72  49  70]\n",
      " [ 51  38 137  63 348  40 144  95  41  43]\n",
      " [ 56  46  71 144 160 219 128  74  54  48]\n",
      " [ 36  38  81 100 222  64 347  42  24  46]\n",
      " [ 81  60  59  80 193  81  74 208  46 118]\n",
      " [135  90  38  52  45  44  27  18 410 141]\n",
      " [ 85 152  25  64  59  36  45  35 113 386]]\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "           0       0.40      0.46      0.43      1000\n",
      "           1       0.36      0.32      0.34      1000\n",
      "           2       0.30      0.25      0.27      1000\n",
      "           3       0.19      0.16      0.17      1000\n",
      "           4       0.22      0.35      0.27      1000\n",
      "           5       0.30      0.22      0.25      1000\n",
      "           6       0.30      0.35      0.32      1000\n",
      "           7       0.31      0.21      0.25      1000\n",
      "           8       0.40      0.41      0.40      1000\n",
      "           9       0.33      0.39      0.36      1000\n",
      "\n",
      "    accuracy                           0.31     10000\n",
      "   macro avg       0.31      0.31      0.31     10000\n",
      "weighted avg       0.31      0.31      0.31     10000\n"
     ]
    }
   ],
   "source": [
    "# 创建决策树模型\n",
    "clf = DecisionTreeClassifier(max_depth=10, random_state=42)\n",
    "\n",
    "# 模型训练\n",
    "start_time = time.time()\n",
    "clf.fit(train_data, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "print(f\"训练时间: {train_time:.2f}秒\")\n",
    "\n",
    "# 模型预测\n",
    "y_pred = clf.predict(test_data)\n",
    "\n",
    "# 模型评估\n",
    "accuracy = accuracy_score(test_labels, y_pred)\n",
    "precision, recall, f1, _ = precision_recall_fscore_support(test_labels, y_pred, average='weighted')\n",
    "conf_matrix = confusion_matrix(test_labels, y_pred)\n",
    "class_report = classification_report(test_labels, y_pred)\n",
    "\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall:\", recall)\n",
    "print(\"F1-score:\", f1)\n",
    "print(\"Confusion Matrix:\\n\", conf_matrix)\n",
    "print(\"Classification Report:\\n\", class_report)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-05-22T13:17:45.133735Z",
     "start_time": "2025-05-22T13:17:29.650194Z"
    }
   },
   "id": "3bf3d01dc14f4cd2",
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 使用热力图展示混淆矩阵"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ceba1450d03d5e97"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 绘制混淆矩阵热力图\n",
    "plt.figure(figsize=(12, 8))\n",
    "sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')\n",
    "plt.title(\"Decision Tree - Confusion Matrix (CIFAR-10)\")\n",
    "plt.xlabel(\"Predicted Label\")\n",
    "plt.ylabel(\"True Label\")\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "25ca4dcf322a2444"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
